from django.shortcuts import render,redirect
from django.http import JsonResponse
from store.models import *
# Create your views here.

# def setcookie(obj):
#     def inner(urls):
#         response = redirect(urls)
#         response.set_cookie('username', obj.user_name)
#         response.set_cookie('id', obj.id)
#         return response
#     return inner

# 检测cookie装饰器
def checkcookie(func):
    def myview(request, *args, **kwargs):
        user_name = request.COOKIES.get('username')
        user_id = request.COOKIES.get('id')
        user = UserLogin.objects.filter(user_name=user_name, id=user_id).first()
        if user:
            return func(request, *args, **kwargs)
        else:
            return redirect('/store/login/')
    return myview

# 注册函数
def register(request):
    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
        repassword = request.POST.get('repassword')
        blank = username and password
        same = password == repassword
        if blank and same:
            u = UserLogin()
            u.user_name = username
            u.password = password
            u.save()
            return render(request, 'store/login.html', locals())
    return render(request, 'store/register.html')

# 登录函数
def login(request):
    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
        blank = username and password
        if blank:
            u = UserLogin.objects.filter(user_name=username).first()
            if u and u.password == password:
                response = redirect('/store/index/')
                response.set_cookie('username', u.user_name)
                response.set_cookie('id', u.id)
                return response
            return render(request, 'store/register.html', locals())
    return render(request, 'store/login.html')

# 登出函数
def logout(request):
    response = redirect('/store/login/')
    response.delete_cookie('id')
    response.delete_cookie('username')
    return response

# 显示主页函数
@checkcookie
def index(request):
    return render(request, 'store/index.html')

# 添加商品页函数
@checkcookie
def goodstype(request):
    if request.method == "POST":
        t = GoodsType()
        t.goods_type_name = request.POST.get("t_name")
        t.goods_image = request.FILES.get("t_pic")
        t.save()
        return redirect('/store/goodstype/')

    g_type = GoodsType.objects.all()
    return render(request, 'store/goodstype.html', locals())

# 删除函数
def deltype(request,t_id):
    t = GoodsType.objects.get(id=t_id)
    t.delete()
    return redirect('/store/goodstype/')

# 修改函数
def changetype(request):
    if request.method == 'GET':
        t_id = request.GET.get('t_id')
        t = GoodsType.objects.get(id=t_id)
        t1 = {
            'id': t.id,
            'type': t.goods_type_name,
        }
        return JsonResponse({'t': t1})
    elif request.method == "POST":
        t_id = request.POST.get('t_id')
        t = GoodsType.objects.get(id=t_id)
        t.goods_type_name = request.POST.get("t_name")
        t.goods_image = request.FILES.get("t_pic")
        t.save()
        return redirect('/store/goodstype/')

# 新用户创建店铺
def addstore(request):
    return render(request, 'store/newuser_addstore.html')

# 老用户显示店铺
def showstore(request):
    u_id = request.COOKIES.get('id')
    user = UserLogin.objects.get(id=u_id)
    u_store = user.shop
    return render(request, 'store/store.html', locals())

